package com.bbs.service.impl;

import com.bbs.domain.Report;
import com.bbs.domain.Word;
import com.bbs.entity.Result;
import com.bbs.mapper.ArticleMapper;
import com.bbs.domain.Article;
import com.bbs.entity.PageResult;
import com.bbs.mapper.UpvoteMapper;
import com.bbs.mapper.WordMapper;
import com.bbs.service.ArticleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;


import java.util.*;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UpvoteMapper upvoteMapper;

    @Autowired
    private WordMapper wordMapper;


    @Override
    public List<Article> findAll() {
        return articleMapper.selectAll();
    }

    @Override
    public PageResult<Article> findByPage(Map searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Article> articles = (Page<Article>) articleMapper.selectByExample(example);
        return new PageResult<Article>(articles.getTotal(), articles.getResult());
    }

    private Example createExample(Map searchMap) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 标题
            if (searchMap.get("title") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 发帖人
            if (searchMap.get("sendername") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }
        }
        return example;
    }


    /**
     * 逻辑删除帖子
     *
     * @param article
     */
    @Override
    public void delete(Article article) {

        articleMapper.deleteByPrimaryKey(article);
    }

    /**
     * 查找单个帖子
     *
     * @param report
     * @return
     */
    @Override
    public Article findOne(Report report) {
        return articleMapper.selectByPrimaryKey(report.getArticleid());
    }

    /**
     * 修改帖子的置顶状态
     *
     * @param article
     */
    @Override
    public void updateIsTop(Article article) {

        articleMapper.updateByPrimaryKeySelective(article);
    }

    /**
     * 新发表帖子
     *
     * @param article
     * @param zoneId
     */
    @Override
    public Result add(Article article, String zoneId) {
        if (article.getTitle() == null) {
            return new Result(1, "帖子没有标题");
        }
        if (zoneId == null) {
            zoneId = "1";
        }
        //查询所有敏感词
        Example example = new Example(Word.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",1);//启用状态
        List<Word> words = wordMapper.selectByExample(example);
        List<String> woedList = new ArrayList<>();
        for (Word word : words) {
            woedList.add(word.getWord());
        }
        String content = article.getContent();

        for (String word : woedList) {
            content = content.replace(word, "**");

        }
        article.setContent(content);

        article.setZoneid(Integer.parseInt(zoneId));

//        articleMapper.insert(article);

        articleMapper.insertSelective(article);
        List<Article> select = articleMapper.select(article);
        if(select==null){
            return new Result(1, "发表帖子失败");
        }
        Integer articleid = select.get(0).getArticleid();
        Result result = new Result();
        result.setCode(0);
        result.setMessage("发表帖子成功");
        result.setOther(articleid);
        return new Result();
    }


    /**
     * 根据id查询发帖次数
     *
     * @param username
     * @return
     */
    @Override
    public int findCountByUsername(String username) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sendername", username);
        int count = articleMapper.selectCountByExample(example);

        return count;
    }

    /**
     * 根据帖子id查询帖子
     *
     * @param articleid 帖子id
     * @return
     */
    @Override
    public Article findById(Integer articleid) {
        return articleMapper.selectByPrimaryKey(articleid);
    }



    /**
     * 查询当天发帖次数
     *
     * @param
     * @return
     */
    @Override
    public int AllPostsToday() {
        // 获取当天凌晨0点0分0秒Date
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        Date beginOfDate = calendar1.getTime();
//        System.out.println(beginOfDate);
        // 获取当天23点59分59秒Date
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        Date endOfDate = calendar1.getTime();
//        System.out.println(endOfDate);

        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThan("sendtime" ,endOfDate);
        criteria.andGreaterThan("sendtime" ,beginOfDate);
        List<Article> articles = articleMapper.selectByExample(example);

        return articles.size();
    }

    /**
     * 根据所在区查询帖子列表
     * @param zoneId 帖子所属区id
     * @return
     */
    @Override
    public List<Article> findArticleListByZoneId(Integer zoneId) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("zoneid",zoneId);
        example.setOrderByClause("isTop DESC");
        List<Article> articleList = articleMapper.selectByExample(example);
        return articleList;
    }

    /**
     * 根据关键字查询帖子
     * @param keyword
     * @return
     */
    @Override
    public List<Article> findArticleByKeyword(String keyword) {
        if (StringUtils.isEmpty(keyword)){
            throw new RuntimeException("404");
        }
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orLike("title","%"+keyword+"%");
        criteria.orLike("sendername","%"+keyword+"%");
        criteria.orLike("content","%"+keyword+"%");
        example.setOrderByClause("isTop DESC");
        List<Article> articleList = articleMapper.selectByExample(example);
        return articleList;
    }


}
